package
{
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	import flash.utils.Dictionary;
	
	import popple.display.DisplayObject;
	import popple.display.Sprite;
	import popple.display.TBitmap;
	
	public class Particle extends Sprite
	{
		private var _pos:Vector3D;
		private var _tx:Number;
		private var _ty:Number;
		private var _tz:Number;
		private var _isWork:Boolean;
		private var _speed:Number=10;
		private var _grative:Number=0;
		private var _z:int;
		public function Particle(bitmap:TBitmap=null)
		{
			super(bitmap);
		}
		
		public function move(position:Vector3D):void
		{
			work(true);
			_pos=position;
			var radius:Number=Math.sqrt(Math.random())*720;
			var angle:Number=Math.random()*(Math.PI*2);
			var tx:Number=Math.cos(angle)*radius;
			var ty:Number=Math.sin(angle)*radius;
			var tz:Number=Math.cos(angle)*radius;
			_tx=_pos.x+tx;
			_ty=_pos.y+ty;
			_tz=_pos.z+tz;
			//_pos.z=Math.random()*100;
			this.x=_pos.x;
			this.y=_pos.y;
			this.z=_pos.z;
			this.alpha=1;
			_speed=Math.random()*20;
			addEventHandle("enterFrame",onEnter);
		}
		private function onEnter(e:DisplayObject):void
		{
			var tx:Number=(_tx-this.x)/_speed;
			var ty:Number=(_ty-this.y)/_speed;
			var tz:Number=(_tz-this.z)/_speed;
			
			_grative+=.1;
			
			this.x+=tx;
			this.y+=ty+_grative;
			this.z+=tz;
			_z+=Math.random()*5;
			this.rotationZ=_z;
			var test:Boolean=Math.abs(tx)>0.1;
			if(test==false)
			{
				var ta:Number=(0-this.alpha)/2;
				this.alpha+=(ta+Math.random()*.1);
				
				if(this.alpha<.1)
				{
					_grative=0;
					removeEventHandle("enterFrame",onEnter);
				    work(false);
				}
				
			
			}
			
			
		}
		public function isWorking():Boolean
		{
			return _isWork;
		}
		private function work(value:Boolean):void
		{
			_isWork=value;
			if(_isWork==false)
			{
				if(this.parent!=null)
				{
					this.parent.removeChild(this);
				}
			}
		}
	}
}